package com.it.jzs.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.jzs.dao.admin.AdminLinkMapper;
import com.it.jzs.pojo.Link;
import com.it.jzs.pojo.Type;
import com.it.jzs.service.admin.AdminLinkService;
import com.it.jzs.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminLinkServiceImpl implements AdminLinkService {
    @Resource
    private AdminLinkMapper adminLinkMapper;

    @Override
    public Result<Object> selectPage(Integer index, Integer size) {

        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        Page<Link> page = new Page<>(index, size);

        Page<Link> typePage = adminLinkMapper.selectPage(page, wrapper);
        return Result.success(typePage, "获取分类分页数据成功");
    }

    @Override
    public Result<Object> insertOne(Link link) {
        String linkDescriptionr = link.getLinkDescription();
        String linkName = link.getLinkName();
        String linkA = link.getLinkA();
//        link.get
        if (StringUtils.isEmpty(linkDescriptionr) || StringUtils.isEmpty(link) || StringUtils.isEmpty(linkA)) {
            return Result.error(null, "数据不合法");
        }
        link.setCreateTime(System.currentTimeMillis());
        link.setUpdateTime(System.currentTimeMillis());
        int insert = adminLinkMapper.insert(link);
        if (insert > 0) {
            return Result.success(null, "新增友链成功");
        }
        return Result.error(null, "新增友链失败");
    }

    @Override
    @Transactional
    public Result<Object> delete(Integer id) {
        if (id != null) {
//                Type type = typeMapper.selectById(id);
            int i = adminLinkMapper.deleteById(id);
            if (i > 0) {
                return Result.success(null, "删除成功！");
            }
        }
        return Result.error(null, "删除失败！");
    }

    @Override
    @Transactional
    public Result<Object> update(Link link) {  // 修改分类

        String linkDescriptionr = link.getLinkDescription();
        String linkName = link.getLinkName();
        String linkA = link.getLinkA();
//        link.get
        if (StringUtils.isEmpty(linkDescriptionr) || StringUtils.isEmpty(link) || StringUtils.isEmpty(linkA)) {
            return Result.error(null, "数据不合法");
        }
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        link.setUpdateTime(System.currentTimeMillis());
        int update = adminLinkMapper.updateById(link);
        if (update > 0) {
            return Result.success(null, "修改成功");
        }
        return Result.error(null, "修改失败");
//        int i = typeMapper.updateById(type);


    }

    @Override
    public Result<Object> selectAll() {

        List<Link> links = adminLinkMapper.selectList(null);
        return Result.success(links, "获取友链数据成功！");

    }
}
